﻿using System;
using System.Collections.Generic;
using System.Text;
using TVOnline.Entities;
using EricUtility.Networking.Gathering;
using EricUtility;

namespace WatchMoviesHelper.Entities
{
    public abstract class AbstractMovieList : AbstractLoadingTuple
    {
        protected abstract string BaseURL { get; }

        protected readonly string m_Name;
        protected readonly List<MovieInfo> m_Movies = new List<MovieInfo>();

        public string Name { get { return m_Name; } }
        public MovieInfo[] Movies { get { return m_Movies.ToArray(); } }

        public AbstractMovieList(string name)
        {
            m_Name = name;
        }
        protected void AddShow(MovieInfo s)
        {
            m_Movies.Add(s);
        }
        protected override void OnLoad()
        {
            m_Movies.Clear();
            string src = GatheringUtility.GetPageSource(BaseURL + m_Name);
            src = src.Substring(src.IndexOf("<ul class=\"listings\">"));
            string showIdDeb = "<a href=\"http://watch-movies-az.com/movie/";
            string guillFin = "\"";
            string showNameDeb = ">";
            string showNameFin = "</a>";
            int posShowDeb = 0;
            while ((posShowDeb = src.IndexOf(showIdDeb)) >= 0)
            {
                src = src.Substring(posShowDeb + showIdDeb.Length);
                int posShowIdFin = src.IndexOf(guillFin);
                string id = src.Substring(0, posShowIdFin);

                int posShowNameDeb = src.IndexOf(showNameDeb, posShowIdFin) + showNameDeb.Length;
                int posShowNameFin = src.IndexOf(showNameFin, posShowNameDeb);
                string name = src.Substring(posShowNameDeb, posShowNameFin - posShowNameDeb);

                AddShow(new MovieInfo(id, name));
            }
        }
    }
}
